<?php

/**
 * Scrum
 *
 * @date 2018-07-31
 */

namespace App\Web\Controller;

use \Lib;
use \App;

class Controller_Scrum extends \Lib\Controller {
    private $objScrum;

    function __construct() {
        $this->objScrum = new  App\Web\Model\Model_Scrum();
    }

    public function index() {
        $sid = Lib\Http::get('sid');

        if (empty($sid)) {
            $sprintInfo = $this->objScrum->getLastSprintInfo();
            if (empty($sprintInfo)) {
                echo "没有冲刺～";
                exit;
            }
            $sid = $sprintInfo['id'];
        } else {
            $sprintInfo = $this->objScrum->getSprintsInfoBySid($sid);
        }

        //冲刺列表
        $sprintsList = $this->objScrum->getSprintsList();
        if (!empty($sprintsList)) {
            $tmpArr = array();
            foreach ($sprintsList AS $k => $v) {
                $tmpArr[$k]['id']   = $v['id'];
                $tmpArr[$k]['name'] = $v['name'];
            }
        }
        $sprintsList  = $tmpArr;
        $todoList     = $this->objScrum->getTasksByStatus(0, $sid);
        $progressList = $this->objScrum->getTasksByStatus(1, $sid);
        $verifyList   = $this->objScrum->getTasksByStatus(2, $sid);
        $doneList     = $this->objScrum->getTasksByStatus(3, $sid);

        $data = array(
            "sid"          => $sid,
            "sprintInfo"   => $sprintInfo,
            "sprintsList"  => $sprintsList,
            "todoList"     => $todoList,
            "progressList" => $progressList,
            "verifyList"   => $verifyList,
            "doneList"     => $doneList,
            "staticPath"   => Static_PATH,
        );

        $this->assign($data);

        $this->display('scrum/scrumList.html');
    }

    //添加冲刺
    public function addSprint() {
        $sprintName = Lib\Http::post("sprint_name");
        $sid        = $this->objScrum->addSprint(array("name" => $sprintName, "addtime" => time()));

        echo json_encode(array('status' => 1, 'sid' => $sid, 'message' => '添加冲刺成功'));
        exit;
    }

    //编辑冲刺
    public function editSprint() {
        $newName = Lib\Http::post("new_name");
        $sid     = Lib\Http::post("sid");
        $where   = [
            [
                'filed' => 'id',
                'data'  => $sid,
            ],
        ];
        $data    = array("name" => $newName);
        $this->objScrum->editSprint($where, $data);
        echo json_encode(array('status' => 1, 'message' => '修改成功'));
    }

    //删除冲刺
    public function delSprint() {
        $sid        = Lib\Http::post("sid");
        $sprintInfo = $this->objScrum->getSprintsInfoBySid($sid);
        if ($sprintInfo) {
            $where = array('id' => $sid);
            $this->objScrum->delSprint($where);
            echo json_encode(array('status' => 1, 'message' => '删除成功'));
        } else {
            echo json_encode(array('status' => -1, 'message' => '不存在'));
        }
        exit;
    }

    //添加任务
    public function addtask() {
        $taskName  = Lib\Http::post("task_name");
        $taskColor = Lib\Http::post("task_color");
        $taskSid   = Lib\Http::post("task_sid");

        $taskNum = $this->objScrum->getTasksNumByStatus(0, $taskSid);

        $data = array(
            "task_name" => $taskName,
            "sid"       => $taskSid,
            "bgcolor"   => $taskColor,
            "task_sort" => $taskNum,
            "addtime"   => time(),
        );
        $this->objScrum->addTask($data);
        echo json_encode(array('status' => 1, 'message' => '添加任务成功'));
        exit;
    }

    //删除任务
    public function deltask() {
        $taskId   = Lib\Http::post("taskid");
        $taskInfo = $this->objScrum->getTaskInfoBySid($taskId);
        if ($taskInfo) {
            $where = array('id' => $taskId);
            $this->objScrum->delTask($where);
            echo json_encode(array('status' => 1, 'message' => '删除任务成功'));
        } else {
            echo json_encode(array('status' => -1, 'message' => '任务不存在'));
        }
        exit;
    }

    //编辑颜色
    public function edittask() {
        $taskId       = Lib\Http::post("task_id");
        $task_bgcolor = Lib\Http::post("task_bgcolor");
        $data         = array("bgcolor" => $task_bgcolor);
        $this->objScrum->editTask($taskId, $data);
        echo json_encode(array('status' => 1, 'message' => '修改成功'));
    }

    //编辑名字
    public function ediTaskName() {
        $taskId   = Lib\Http::post("task_id");
        $taskName = Lib\Http::post("task_name");
        $data     = array("task_name" => $taskName);
        $this->objScrum->editTask($taskId, $data);
        echo json_encode(array('status' => 1, 'message' => '修改成功'));
    }

    //拖动顺序
    public function sortask() {
        $todoIDs     = Lib\Http::post("todoIDs");
        $progressIDs = Lib\Http::post("progressIDs");
        $verifyIDs   = Lib\Http::post("verifyIDs");
        $doneIDs     = Lib\Http::post("doneIDs");

        //Todo
        foreach ($todoIDs AS $k => $v) {
            $this->objScrum->editTask($v, array("status" => 0, "task_sort" => $k));
        }
        //Progress
        foreach ($progressIDs AS $k => $v) {
            $this->objScrum->editTask($v, array("status" => 1, "task_sort" => $k));
        }
        //Verify
        foreach ($verifyIDs AS $k => $v) {
            $this->objScrum->editTask($v, array("status" => 2, "task_sort" => $k));
        }
        //Done
        foreach ($doneIDs AS $k => $v) {
            $this->objScrum->editTask($v, array("status" => 3, "task_sort" => $k));
        }
        echo json_encode(array('status' => 1, 'message' => '修改成功'));
    }
}